﻿package mortal.game.view.copy.defenseCopy
{
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.button.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.copy.defenseCopy.view.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class DefenseCopyBag extends SmallWindow
    {
        private var _list:GTileList;
        private var _txtCapacity:GTextFiled;
        private var _btnShop:GLabelButton;
        private var _btnSell:GLabelButton;
        private var _btnTidy:TimeButton;
        private var _txtDefenseMoney:GTextFiled;
        private var _uiInitOver:Boolean = false;
        private static var _instance:DefenseCopyBag;

        public function DefenseCopyBag(param1:ILayer = null)
        {
            super(param1);
            setSize(337, 370);
            title = "" + Language.getString(70861);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._list = UIFactory.tileList(12, 35, 320, 260, this);
            this._list.rowHeight = 43;
            this._list.columnWidth = 43;
            this._list.direction = GBoxDirection.VERTICAL;
            this._list.setStyle("cellRenderer", DefenseBagItemCellRenderer);
            this._list.selectable = false;
            pushUIToDisposeVec(UIFactory.bg(10, 303, 323, 40, this));
            pushUIToDisposeVec(UIFactory.textField("" + Language.getString(70862), 15, 314, 63, 20, this, GlobalStyle.textFormatHuang));
            this._txtCapacity = UIFactory.textField("415/12312", 15 + 63, 314, 145, 20, this);
            this._btnShop = UIFactory.gLabelButton(ImagesConst.SanTxt, GLabelButton.gLoadedButton, ImagesConst.PackBtn_upSkin, 127, 312, 51, 24, this);
            this._btnShop.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._btnSell = UIFactory.gLabelButton(ImagesConst.MaiTxt, GLabelButton.gLoadedButton, ImagesConst.PackBtn_upSkin, 198, 312, 51, 24, this);
            this._btnSell.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._btnSell.name = "defenseCopySellBtn";
            this._btnTidy = UIFactory.timeButton("", 267, 312, 51, 24, this, "DefenseCopyTidy", "PackButton");
            this._btnTidy.cdTime = 10000;
            this._btnTidy.labelBitmap = ImagesConst.ZenTxt;
            this._btnTidy.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._txtDefenseMoney = UIFactory.textField(Language.getString(70863) + "3000", 185, 342, 145, 20, this, GlobalStyle.textFormatHuang2.right());
            this.configEventListener(DragEvent.Event_Throw_goods, this.itemThrowHandler);
            this.configEventListener(MouseEvent.MOUSE_OVER, this.onRollOver);
            this.configEventListener(MouseEvent.ROLL_OUT, this.onRollOut);
            this._uiInitOver = true;
            this.updateAll();
            return;
        }// end function

        public function onClickHandler(event:MouseEvent) : void
        {
            if (this._uiInitOver == false)
            {
                return;
            }
            switch(event.currentTarget)
            {
                case this._btnShop:
                {
                    CursorManager.currentCurSorType = CursorManager.Arrow;
                    CursorManager.showCursor(CursorManager.Arrow);
                    LayerManager.windowLayer.tweenCenterWindow(DefenseCopyShopNew.instance, DefenseCopyBag.instance);
                    break;
                }
                case this._btnSell:
                {
                    if (CursorManager.currentCurSorType == CursorManager.DEFENSE_COPY_SELL)
                    {
                        CursorManager.currentCurSorType = CursorManager.Arrow;
                        CursorManager.showCursor(CursorManager.Arrow);
                    }
                    else
                    {
                        CursorManager.currentCurSorType = CursorManager.DEFENSE_COPY_SELL;
                        CursorManager.showCursor(CursorManager.DEFENSE_COPY_SELL);
                    }
                    break;
                }
                case this._btnTidy:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyTidy));
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function onRollOver(event:MouseEvent) : void
        {
            if (CursorManager.currentCurSorType == CursorManager.DEFENSE_COPY_SELL)
            {
                CursorManager.showCursor(CursorManager.SELL);
            }
            return;
        }// end function

        private function onRollOut(event:MouseEvent) : void
        {
            if (CursorManager.currentCurSorType == CursorManager.DEFENSE_COPY_SELL)
            {
                CursorManager.showCursor(CursorManager.Arrow);
            }
            return;
        }// end function

        public function updateAll(param1:Object = null) : void
        {
            if (Cache.instance.copy.defenseCopy.copyReward)
            {
                this._txtDefenseMoney.text = "" + Language.getString(70863) + Cache.instance.copy.defenseCopy.copyReward.equipPoint.toString();
            }
            if (Boolean(param1) == false)
            {
                this.updateList();
            }
            return;
        }// end function

        private function updateList() : void
        {
            var _loc_4:int = 0;
            var _loc_7:int = 0;
            var _loc_1:* = new DataProvider();
            var _loc_2:* = Cache.instance.pack.defenseBag.allItems;
            var _loc_3:* = _loc_2.length;
            var _loc_5:* = _loc_3 - 1;
            while (_loc_5 > 0)
            {
                
                if (_loc_2[_loc_5])
                {
                    _loc_7 = (_loc_5 + 1) % 7;
                    if (_loc_7 > 0)
                    {
                        _loc_4 = (_loc_5 + 1) + (7 - _loc_7);
                    }
                    else
                    {
                        _loc_4 = _loc_5 + 1;
                    }
                    break;
                }
                _loc_5 = _loc_5 - 1;
            }
            if (_loc_4 < 42)
            {
                _loc_4 = 42;
            }
            var _loc_6:int = 0;
            while (_loc_6 < _loc_4)
            {
                
                if (_loc_6 < _loc_2.length)
                {
                    _loc_1.addItem(_loc_2[_loc_6]);
                }
                else
                {
                    _loc_1.addItem(new Object());
                }
                _loc_6++;
            }
            this._list.dataProvider = _loc_1;
            this._list.drawNow();
            this._txtCapacity.text = Cache.instance.pack.defenseBag.itemLength + "/" + _loc_4;
            return;
        }// end function

        public function getItemPointByCode(param1:int) : Point
        {
            var _loc_3:ItemData = null;
            var _loc_4:DefenseBagItemCellRenderer = null;
            var _loc_6:Point = null;
            var _loc_7:Point = null;
            var _loc_2:* = this._list.length;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_2)
            {
                
                _loc_3 = this._list.getItemAt(_loc_5) as ItemData;
                if (_loc_3 && _loc_3.itemCode == param1)
                {
                    _loc_4 = this._list.itemToCellRenderer(_loc_3) as DefenseBagItemCellRenderer;
                    _loc_6 = this.localToGlobal(GameConst.yellowPoint);
                    _loc_7 = _loc_4.localToGlobal(GameConst.yellowPoint);
                    _loc_7.x = _loc_7.x - _loc_6.x + 6;
                    _loc_7.y = _loc_7.y - _loc_6.y + 6;
                    return _loc_7;
                }
                _loc_5++;
            }
            return null;
        }// end function

        public function getItemPointByUid(param1:String) : Point
        {
            var _loc_3:ItemData = null;
            var _loc_4:DefenseBagItemCellRenderer = null;
            var _loc_2:* = this._list.length;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_2)
            {
                
                _loc_3 = this._list.getItemAt(_loc_5) as ItemData;
                if (_loc_3 && _loc_3.uid == param1)
                {
                    _loc_4 = this._list.itemToCellRenderer(_loc_3) as DefenseBagItemCellRenderer;
                    return _loc_4.parent.localToGlobal(new Point(_loc_4.x, _loc_4.y));
                }
                _loc_5++;
            }
            return null;
        }// end function

        private function itemThrowHandler(event:DragEvent) : void
        {
            var _loc_2:* = event.dragItem as BaseItem;
            Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyThrowEquip, _loc_2.itemData.uid));
            return;
        }// end function

        override protected function configParams() : void
        {
            paddingBottom = 75;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._uiInitOver = false;
            CursorManager.currentCurSorType = CursorManager.Arrow;
            CursorManager.showCursor(CursorManager.Arrow);
            paddingBottom = 75;
            this._btnSell.name = "";
            this._list.dispose(param1);
            this._txtCapacity.dispose(param1);
            this._btnShop.dispose(param1);
            this._btnSell.dispose(param1);
            this._btnTidy.dispose(param1);
            this._txtDefenseMoney.dispose(param1);
            this._list = null;
            this._txtCapacity = null;
            this._btnShop = null;
            this._btnSell = null;
            this._btnTidy = null;
            this._txtDefenseMoney = null;
            return;
        }// end function

        public static function get hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

        public static function get instance() : DefenseCopyBag
        {
            if (_instance == null)
            {
                _instance = new DefenseCopyBag;
            }
            return _instance;
        }// end function

    }
}
